package com.kirk.cts.ui.view.panel;

import com.kirk.cts.core.event.CrawlerToolEvent;
import com.kirk.cts.core.event.CrawlerToolEventType;
import com.kirk.cts.core.event.CrawlerToolObserver;
import com.kirk.cts.ui.listener.VerifierListener;
import org.apache.log4j.Logger;

import javax.swing.*;
import java.awt.*;

/**
 * <p> AbstractContentSettingPanel.java </p>
 *
 * @version 1.0.0
 * @Author kirkliu
 * @Date 2020-09-01 23:16
 * @Description 抽象内容设置面板, 主要用于在主窗口中显示
 */
public abstract class AbstractContentSettingPanel<T> extends JPanel implements IPage, VerifierListener, IContentPanel<T> {
    private static final long serialVersionUID = 1L;
    protected Logger logger = Logger.getLogger(this.getClass());
    private boolean hasInit = false;
    private boolean isDefaultPage;
    protected JPanel buttonBar;

    protected AbstractContentSettingPanel() {
        super();
    }

    public void init() {
        if (!hasInit) {
            BorderLayout layout = new BorderLayout();
            this.setLayout(layout);

            initComponents();
            initActionListener();

            if (getBottomPane() != null) {
                this.add(getBottomPane(), BorderLayout.SOUTH);
            }
            if (getCenterPane() != null) {
                this.add(getCenterPane(), BorderLayout.CENTER);
            }

            CrawlerToolObserver.getInstance().addCrawlerListener(this);
            hasInit = true;
        }
        initData(null);
    }

    protected Component getBottomPane() {
        return buttonBar;
    }

    protected Component getCenterPane() {
        return null;
    }

    protected Component getButtonBar() {
        if (buttonBar == null) {
            buttonBar = new JPanel();
            FlowLayout layou = new FlowLayout(FlowLayout.LEADING, 3, 2);
            layou.setAlignment(FlowLayout.RIGHT);
            buttonBar.setLayout(layou);
        }
        return buttonBar;
    }

    /**
     * 初始化事件
     */
    protected void initActionListener() {

    }

    /**
     * 更新监听执行事件
     */
    public void update(CrawlerToolEvent event) {

    }

    /**
     * 失败时调用
     */
    @Override
    public void invalidData(String message, JComponent component) {
        CrawlerToolObserver.getInstance().notifyEvents(new CrawlerToolEvent(this, CrawlerToolEventType.ValidDataChangeEvent, message));
        getToolkit().beep();
    }

    public void validData(JComponent jComponent) {
        CrawlerToolObserver.getInstance().notifyEvents(new CrawlerToolEvent(this, CrawlerToolEventType.ValidDataChangeEvent, ""));
    }

    /**
     * 初始化数据
     */
    public void initData(T t) {

    }

    ;

    /**
     * 取得内容对象
     */
    public final T getData() {
        return populateData();
    }

    /**
     * 完成销毁动作
     */
    public void dispose() {

    }

    /**
     * 组装数据
     */
    protected abstract T populateData();

    /**
     * 初始化组件
     */
    protected abstract void initComponents();

    /**
     * 填充页面控件数据
     */
    protected abstract void fillComponentData(T t);

    public String getPageId() {
        return this.getClass().getName();
    }

    public String getPageName() {
        return this.getClass().getSimpleName();
    }

    public void disposePage() {
        this.dispose();
        CrawlerToolObserver.getInstance().removeCrawlerListener(this);
    }

    @Override
    public boolean isDefaultPage() {
        return isDefaultPage;
    }

    @Override
    public void setDefaultPage(boolean bool) {
        isDefaultPage = bool;
    }

    public void showData(int pageNumber, int pageSize) {

    }
}
